package com.leemon.mall.security.util;

import com.leemon.mall.security.dto.MySysUser;
import lombok.experimental.UtilityClass;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author limenglong
 * @create 2019-09-19 17:23
 * @desc 安全工具类
 **/
@UtilityClass
public class SecurityUtils {

    //获取Authentication
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public MySysUser getSysUser(){
        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof MySysUser){
            return (MySysUser) principal;
        }
        throw new AuthenticationCredentialsNotFoundException("无法获取系统用户信息");
    }
}
